<template>
    <CommonModal :isVisible="isVisible" :backgroundImage="modal2Png" @close="closeModal"
        :modalStyle="{ width: '1040px', height: '823px', minWidth: '1040px' }">
        <template #content>
            <!-- 输入框区域 -->
            <div class="input-area">
                <input v-model="nickname" type="text" placeholder="昵称" class="nickname-input"
                    @keyup.enter="handleLogin" />
            </div>
        </template>

        <template #buttons>
            <!-- 登录按钮 -->
            <div class="login-btn" @click="handleLogin"></div>
        </template>
    </CommonModal>
</template>

<script setup>
import { ref, defineEmits, watch } from 'vue'
import CommonModal from './CommonModal.vue'
import modal2Png from '@/assets/images/2/modal.png'

const props = defineProps({
    isVisible: {
        type: Boolean,
        default: false
    }
})

watch(() => props.isVisible, (newVal) => {
    if (!newVal) {
        nickname.value = ''
    }
})

const emit = defineEmits(['close', 'login'])

const nickname = ref('')

const closeModal = () => {
    emit('close')
}

const handleLogin = () => {
    if (nickname.value.trim()) {
        // 保存昵称到本地存储
        localStorage.setItem('userNickname', nickname.value.trim())
        emit('login', nickname.value.trim())
        closeModal()
    }
}
</script>

<style lang="scss" scoped>
// 输入框区域
.input-area {
    position: absolute;
    bottom: 234px;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 550px;
}

.nickname-input {
    width: 100%;
    height: 72px;
    border: none;
    border-radius: 36px;
    font-size: 32px;
    text-align: center;
    outline: none;
    background: transparent;
    transition: all 0.3s ease;
    color: #4C0000;
    font-weight: 600;

    &:focus {
        background-color: transparent;
    }

    &::placeholder {
        color: #d8d8d8;
        font-size: 32px;
        font-weight: 600;
    }
}

// 登录按钮
.login-btn {
    position: absolute;
    bottom: 150px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    height: 96px;
    background: url('../assets/images/2/login.png') no-repeat center center;
    background-size: contain;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        background-image: url('../assets/images/2/login_active.png');
        transform: translateX(-50%) scale(1.05);
    }

    &:active {
        transform: translateX(-50%) scale(0.95);
    }
}
</style>